package com.aglhz.nature.wxapi;



import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import com.aglhz.nature.R;
import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.AroundViewEvent;
import com.aglhz.nature.modle.AroundBean;
import com.aglhz.nature.modle.CodeBean;
import com.aglhz.nature.utils.AsyncHttpCilentUtil;
import com.aglhz.nature.utils.Constants;
import com.aglhz.nature.view.MainActivity;
import com.aglhz.nature.view.SetActivity;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	private SharedPreferences sharedPreferences;  
	private static final String TAG = WXPayEntryActivity.class.getName();
	private String payinfo;
    private IWXAPI api;
    
    private CodeBean codeBean;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this,Constants.WX_KEY);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
			Log.e("resp.errCode=", "==="+resp.errCode);
			if(resp.errCode == 0){
				updateOrder("1");
			}
			finish();
				
		
		}
	}
	public void updateOrder(String payStatus){

		String url = ServerAPI.APIHEARD+ServerAPI.UPORDER;
		
		Log.e("codeBean", url);
		
		 sharedPreferences =getSharedPreferences("PAY", Context.MODE_PRIVATE);
		 payinfo= sharedPreferences.getString("PAYINFO","");
		 JSONObject json;
		 try {
			json = new JSONObject( payinfo);
			 AsyncHttpClient client =  AsyncHttpCilentUtil.getInstance(WXPayEntryActivity.this);
			 RequestParams params = new RequestParams();  
			 JSONObject payData = json.getJSONObject("data");
			 
			 params.put("lstOrders",json.getString("lstOrders"));
			 Log.e("lstOrders",json.getString("lstOrders"));
			 params.put("cardCode", json.getString("cardCode"));
			 params.put("toUseMemberMoney",json.getString("toUseMemberMoney"));
			 params.put("payStatus", payStatus);
			 params.put("wxOrderSn", payData.getString("wxOrderSn"));
			
			client.post(url,params, new TextHttpResponseHandler() {
				
				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					 try {
						 Log.e(TAG, "response" + responseString);
						
						 Gson gson = new Gson();
						
						 CodeBean cBean = gson.fromJson(responseString,
								 CodeBean.class);
						 codeBean= (CodeBean) cBean.clone();
						
						Log.d(TAG,"---111-----"+codeBean.getOther());	
						int code = Integer.parseInt(codeBean.getOther().getCode());
					if(code==200){
						Toast.makeText(getApplication(), "更新订单状态成功",Toast.LENGTH_SHORT).show();
						Intent intent = new Intent(WXPayEntryActivity.this,MainActivity.class);
						intent.putExtra("payreturn", "payreturn");
						startActivity(intent);
						finish();
					}else{
				 		Toast.makeText(getApplication(), "更新订单状态失败",Toast.LENGTH_SHORT).show();
						Intent intent = new Intent(WXPayEntryActivity.this,MainActivity.class);
						intent.putExtra("payreturn", "payreturn");
						startActivity(intent);
						finish();
					}
						
						 
						 Log.e(TAG, "success" + "success");
						 } catch (Exception e) {
						 e.printStackTrace();
						 }
					
					
				}
				
				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {
					
					 Log.e(TAG, "failure");
				}
			

		

		
			});
			Log.e("ooo","---------");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
}